﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.ResourceManage.Controllers
{
    public class RS_ProductInController : MvcControllerBase
    {
        private WarehouseBLL bll = new WarehouseBLL();

        #region 视图
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit, "入仓单首页", "Index")]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit, "入仓单", "Form")]
        public ActionResult Form()
        {
            ViewBag.Mode = new CodeRuleBLL().CheckBillCodeMode(Convert.ToInt32(CodeRuleEnum.ProductInBill).ToString());
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit, "入仓单审核", "AuditForm")]
        public ActionResult AuditForm()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取入仓单列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="queryJson"></param>
        /// <returns></returns> 
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "数据列表", "GetPageList")]
        public ActionResult GetPageList(Pagination pagination, string queryJson)
        {
            if (!string.IsNullOrEmpty(queryJson))
            {
                var queryParam = queryJson.ToJObject();
                if (!queryParam["OrganizeId"].IsEmpty())
                {
                    string OrganizeId = new OrganizeCache().GetSonOrganizeId(queryParam["OrganizeId"].ToString());
                    queryParam["OrganizeId"] = OrganizeId;
                }
                queryJson = queryParam.ToJson();
            }
            var data = bll.GetWarehouseBillPageList(pagination, queryJson);
            foreach (var item in data)
            {
                item.WarehouseId = bll.GetWarehouseInfoEntity(item.WarehouseId).Name;
            }
            var watch = CommonHelper.TimerStart();
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }
        /// <summary>
        /// 获取明细列表
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "获取明细列表", "GetDetailList")]
        public ActionResult GetDetailList(string keyValue)
        {
            var data = bll.GetWarehouseBillDetailList(keyValue);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 获取单据信息
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "获取单据信息", "GetBillInfo")]
        public ActionResult GetBillInfo(string keyValue)
        {
            var BillDetailList = bll.GetWarehouseBillDetailList(keyValue);
            var Entity = bll.GetDeliveryNoteEntity(keyValue);
            var Data = new
            {
                BillInfo = Entity,
                BillList = BillDetailList
            };
            return Content(Data.ToJson());
        }
        #endregion

        #region 保存数据
        /// <summary>
        /// 保存入仓单
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="queryJson"></param>
        /// <returns></returns> 
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "保存入仓单", "SaveForm")]
        public ActionResult SaveForm(string keyValue, string queryJson)
        {
            try
            {
                bll.SaveWarehouseBillForm(keyValue, queryJson);
                return Success("保存成功!");
            }
            catch (Exception e)
            {
                return Error("保存失败:" + e.Message);
            }

        }

        /// <summary>
        /// 删除入仓单
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Delete, "删除入仓单", "RemoveForm")]
        public ActionResult RemoveForm(string keyValue)
        {
            try
            {
                bll.RemoveDeliveryNoteForm(keyValue);
                return Success("删除成功。");
            }
            catch (Exception e)
            {
                 return Error("删除失败:" + e.Message);
            }
        }
        #endregion

    }
}
